/*
 * CommunicationRobot12.cpp
 *
 *  Created on: 15/11/2013
 *      Author: gonzalo
 */

#include "CommunicationRobot12.h"
#include "../distributed_semaphore/comun/DSEMCommon.h"

namespace middleware {

const std::string CommunicationRobot12::ROBOT12_SEM_PATH = "/tmp/robot12";
const std::string CommunicationRobot12::ROBOT12_SHMEM_PATH = "/tmp/robot12mem";
const char CommunicationRobot12::ROBOT12_SHMEM_KEY_IZQ = 'i';
const char CommunicationRobot12::ROBOT12_SHMEM_KEY_DER = 'd';

CommunicationRobot12::CommunicationRobot12(char key, R12CommunicationType type) : CommunicationRobotPiezas(key) {
	/* Get semaphores by type, just those needed*/
	if (DSEM_ENABLED) {
		switch (type) {
			case ROBOT11:
			case AGV:
				this->semRobot = ipc::Semaphore::get(getSemRobot12FileSignal(), key);
				break;
			case ROBOT12:
				this->semRobot = ipc::Semaphore::get(getSemRobot12FileWait(), key);
				break;
		}
	} else {
		this->semRobot = ipc::Semaphore::get(ROBOT12_SEM_PATH, key);
	}

	/* Get SHMems */
	this->shMemListo = ipc::SharedMemoryInterface<Robot12Listo>::get(ROBOT12_SHMEM_PATH, key,
			DSM_ENABLED ? ((key == ROBOT12_SHMEM_KEY_IZQ)? DSMID_LISTOROBOT12_IZQ : DSMID_LISTOROBOT12_DER) : 0);

	this->shMemEstado = ipc::SharedMemoryInterface<EstadoRobotPiezas>::get(ROBOT12_SHMEM_PATH, CommunicationRobotPiezas::getAltKey(key),
			DSM_ENABLED ? ((key == ROBOT12_SHMEM_KEY_IZQ)? DSMID_EROBOT12_IZQ : DSMID_EROBOT12_DER) : 0);
}

void CommunicationRobot12::setProcesando() {
	this->shMemListo.lock();
	this->shMemListo.write(false);
	this->shMemListo.unlock();
}

void CommunicationRobot12::setListo() {
	this->shMemListo.lock();
	this->shMemListo.write(true);
	this->shMemListo.unlock();	std::string getSemEsperaRobot12Key();
}

bool CommunicationRobot12::estaListo() {
	bool listo;
	this->shMemListo.lock();
	listo = this->shMemListo.read();
	this->shMemListo.unlock();
	return listo;
}

void CommunicationRobot12::create(char key, R12CommunicationType type) {	std::string getSemEsperaRobot12Key();
	/* Get semaphores by type, just those needed*/
	if (DSEM_ENABLED) {
		switch (type) {
			case ROBOT11:
			case AGV:
				ipc::Semaphore::createNonExclusive(getSemRobot12FileSignal(), key).initialize(0);
				break;
			case ROBOT12:
				ipc::Semaphore::createNonExclusive(getSemRobot12FileWait(), key).initialize(0);
				break;
		}
	} else {
		ipc::Semaphore::createNonExclusive(ROBOT12_SEM_PATH, key).initialize(0);
	}

	/* Get SHMems */
	ipc::SharedMemoryInterface<Robot12Listo>::createNonExclusive(ROBOT12_SHMEM_PATH, key);
	ipc::SharedMemoryInterface<EstadoRobotPiezas>tmpMem = ipc::SharedMemoryInterface<EstadoRobotPiezas>::createNonExclusive(ROBOT12_SHMEM_PATH,CommunicationRobotPiezas::getAltKey(key));
	if (!DSM_ENABLED){
		tmpMem.lock();
		tmpMem.write(ROBOT_INACTIVO);
		tmpMem.unlock();
	}
}

void CommunicationRobot12::destroy(char key) {
	ipc::Semaphore::destroyNonExclusive(ROBOT12_SEM_PATH, key);
	ipc::Semaphore::destroyNonExclusive(getSemRobot12FileSignal(), key);
	ipc::Semaphore::destroyNonExclusive(getSemRobot12FileWait(), key);

	ipc::SharedMemoryInterface<Robot12Listo>::destroy(ROBOT12_SHMEM_PATH, key);
	ipc::SharedMemoryInterface<EstadoRobotPiezas>::destroy(ROBOT12_SHMEM_PATH, CommunicationRobotPiezas::getAltKey(key));
}

std::string CommunicationRobot12::getNombre() {
	return "robot 12";
}

CommunicationRobot12::~CommunicationRobot12() {
}

std::string CommunicationRobot12::getSemRobot12FileWait() {
	return ROBOT12_SEM_PATH+"wait";
}

std::string CommunicationRobot12::getSemRobot12FileSignal() {
	return ROBOT12_SEM_PATH+"signal";
}

char CommunicationRobot12::getSemControlRobot12Key(char key) {
	return key;
}

std::string CommunicationRobot12::getSemControlName() {
	return "robot12Control";
}

}
 /* namespace middleware */
